﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace theguineasyndrome
{
    public class Switch : ObstacleElement
    {
        public delegate void OnFunc(Switch switchElement);

        private Texture2D blockTexture;
        private OnFunc onFunc;
        private bool animating;
        private Vector2 initialPosition;
        private bool actionCalled;

        public Switch(Level level, Texture2D texture, Texture2D blockTexture, Vector2 position)
            : base(level, texture, position)
        {
            this.blockTexture = blockTexture;
            onFunc = null;
            initialPosition = position;
            animating = false;
            actionCalled = false;
        }

        public void addBlockToLevel()
        {
            level.addElement(new ObstacleElement(level, blockTexture, new Vector2(position.X - 5, position.Y + 23)));
        }

        public override bool isDynamic()
        {
 	         return true;
        }

        public void setOn(OnFunc onFunc)
        {
            this.onFunc = onFunc;
        }

        public override void notifyCharacterOver(Character character)
        {
            base.notifyCharacterOver(character);

            if (!actionCalled)
                animating = true;
        }

        public override void Update(GameTime gameTime)
        {
            if (animating)
            {
                Position += new Vector2(0, (float)(gameTime.ElapsedGameTime.TotalMilliseconds / 20));

                if (character != null)
                    character.setPosition(position + characterRelativePosition);

                if (Position.Y > initialPosition.Y + 10)
                {
                    Position = new Vector2(Position.X, initialPosition.Y + 10);
                    animating = false;
                    onFunc(this);
                    actionCalled = true;
                }
            }
        }

    }
}
